package com.noahplat.user.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.noahplat.common.utils.NoahEnum;
import com.noahplat.common.utils.RedisUtils;
import com.noahplat.common.utils.Result;
import com.noahplat.common.utils.StatusCode;
import com.noahplat.user.mapper.SconfigMapper;
import com.noahplat.user.pojo.Sconfig;
import com.noahplat.user.service.SconfigService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SconfigServiceImpl implements SconfigService {

    @Autowired
    private SconfigMapper sconfigMapper;
    @Autowired
    private RedisUtils redisUtils;

    /**
     * 查询正在使用的key值ID
     *
     * @return
     */
    @Override
    public Result getMapnum(String type) {
        Sconfig sconfig = sconfigMapper.getMapkey("Y", type);
        if (ObjectUtils.isEmpty(sconfig)) {
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
        return new Result(true, StatusCode.OK, "查询成功", sconfig);
    }

    /**
     * 修改正在使用的key值ID
     *
     * @return
     */
    @Override
    public Result updateMapnum(Long id) {
        int res = sconfigMapper.updateMapid("N", id);
        if (res == 0) {
            return new Result(false, StatusCode.ERROR, "修改失败");
        }
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * 分页获得高德key值信息
     *
     * @param type
     * @param state
     * @param page
     * @param limit
     * @return
     */
    @Override
    public Result getAdminMapkey(String type, String state, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        Example example = new Example(Sconfig.class);
        if (!StringUtils.isEmpty(type)) {
            example.createCriteria().andEqualTo("type", type);
        }
        if (!StringUtils.isEmpty(state)) {
            example.createCriteria().andEqualTo("state", state);
        }
        List<Sconfig> list = sconfigMapper.selectByExample(example);
        if (ObjectUtils.isEmpty(list)) {
            return new Result(false, StatusCode.NOTFOUNDERROR, "暂无数据");
        }
        PageInfo<Sconfig> pageInfo = new PageInfo<>(list);
        return new Result(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /**
     * 通过ID获得高德key详细信息
     *
     * @param id
     * @return
     */
    @Override
    public Result getMapkey(Long id) {
        Sconfig sconfig = sconfigMapper.selectByPrimaryKey(id);
        if (ObjectUtils.isEmpty(sconfig)) {
            return new Result(false, StatusCode.ERROR, "暂无数据");
        }
        return new Result(true, StatusCode.OK, "查询成功");
    }

    /**
     * 修改高德key值信息
     *
     * @param sconfig
     * @return
     */
    @Override
    public Result updateMapkey(Sconfig sconfig) {
        int res = sconfigMapper.updateByPrimaryKeySelective(sconfig);
        if (res == 0) {
            return new Result(false, StatusCode.ERROR, "修改失败");
        }
        if (!sconfig.getState().equals("N")) {
            return new Result(true, StatusCode.OK, "修改成功");
        }
        Result result = playRedis(sconfig);
        if (!result.isFlag()) {
            return new Result(false, StatusCode.ERROR, "修改失败");
        }
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * 批量添加高德KEY信息
     *
     * @param file
     * @return
     */
    @Override
    public Result addMapExcel(MultipartFile file) {
        List<Sconfig> list = new ArrayList<>();
        String fileName = file.getOriginalFilename();
        Workbook workbook = null;
        try {
            InputStream inputStream = file.getInputStream();
            if (fileName.endsWith("xls")) {
                workbook = new HSSFWorkbook(inputStream);
            } else {
                workbook = new XSSFWorkbook(inputStream);
            }
            Sheet sheet = workbook.getSheetAt(0);
            int rowLength = sheet.getLastRowNum() + 1;
            Row row = sheet.getRow(0);
            int colLength = row.getLastCellNum();
            Cell cell = row.getCell(0);
            for (int i = 1; i < rowLength; i++) {
                row = sheet.getRow(i);
                Sconfig sconfig = new Sconfig();
                for (int j = 0; j < colLength; j++) {
                    cell = row.getCell(j);
                    if (cell != null) {
                        cell.setCellType(CellType.STRING);
                        String data = cell.getStringCellValue();
                        data = data.trim();
                        switch (j) {
                            case 0:
                                sconfig.setMapkey(data);
                                break;
                            case 1:
                                sconfig.setState(data);
                                break;
                            case 2:
                                sconfig.setType(data);
                                break;
                            default:
                                break;
                        }
                    }
                }
                list.add(sconfig);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        int res = sconfigMapper.insertMapkey(list);
        if (res == 0) {
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
        return new Result(true, StatusCode.OK, "添加成功");
    }

    /**
     * 删除高德key值
     *
     * @param sconfig
     * @return
     */
    @Override
    public Result delMapkey(Sconfig sconfig) {
        int res = sconfigMapper.deleteByPrimaryKey(sconfig);
        if (res == 0) {
            return new Result(false, StatusCode.ERROR, "删除失败");
        }
        Result result = playRedis(sconfig);
        if (!result.isFlag()) {
            return new Result(false, StatusCode.ERROR, "删除失败");
        }
        return new Result(true, StatusCode.OK, "删除成功");
    }

    public Result playRedis(Sconfig sconfig) {
        if (sconfig.getType().equals("0")) {
            if (redisUtils.hasKey(NoahEnum.MAPKEYDIR.getCode() + NoahEnum.MAPKEY.getCode())) {
                Object object = redisUtils.getValue(NoahEnum.MAPKEYDIR.getCode() + NoahEnum.MAPKEY.getCode());
                Sconfig sconfig1 = (Sconfig) object;
                if (sconfig.getId() == sconfig1.getId()) {
                    redisUtils.del(NoahEnum.MAPKEYDIR.getCode() + NoahEnum.MAPKEY.getCode());
                    if (redisUtils.hasKey(NoahEnum.MAPKEYDIR.getCode() + NoahEnum.MAPNUMKEY.getCode())) {
                        redisUtils.del(NoahEnum.MAPKEYDIR.getCode() + NoahEnum.MAPNUMKEY.getCode());
                    }
                    Sconfig sconfig2 = sconfigMapper.getMapkey("Y", sconfig.getType());
                    if (ObjectUtils.isEmpty(sconfig2)) {
                        return new Result(false, StatusCode.ERROR, "修改失败");
                    }
                    boolean inres = redisUtils.lSet(NoahEnum.MAPKEYDIR.getCode() + NoahEnum.MAPKEY.getCode(), sconfig2);
                    if (!inres) {
                        return new Result(false, StatusCode.ERROR, "修改失败");
                    }
                }
            } else {
                Sconfig sconfig2 = sconfigMapper.getMapkey("Y", sconfig.getType());
                if (ObjectUtils.isEmpty(sconfig2)) {
                    return new Result(false, StatusCode.ERROR, "修改失败");
                }
                boolean inres = redisUtils.lSet(NoahEnum.MAPKEYDIR.getCode() + NoahEnum.MAPKEY.getCode(), sconfig2);
                if (!inres) {
                    return new Result(false, StatusCode.ERROR, "修改失败");
                }
                if (redisUtils.hasKey(NoahEnum.MAPKEYDIR.getCode() + NoahEnum.MAPNUMKEY.getCode())) {
                    redisUtils.del(NoahEnum.MAPKEYDIR.getCode() + NoahEnum.MAPNUMKEY.getCode());
                }
            }
        } else if (sconfig.getType().equals("1")) {
            if (redisUtils.hasKey(NoahEnum.GPSKEYDIR.getCode() + NoahEnum.GPSKEY.getCode())) {
                Object object = redisUtils.getValue(NoahEnum.GPSKEYDIR.getCode() + NoahEnum.GPSKEY.getCode());
                Sconfig sconfig1 = (Sconfig) object;
                if (sconfig.getId() == sconfig1.getId()) {
                    redisUtils.del(NoahEnum.GPSKEYDIR.getCode() + NoahEnum.GPSKEY.getCode());
                    if (redisUtils.hasKey(NoahEnum.GPSKEYDIR.getCode() + NoahEnum.GPSNUMKEY.getCode())) {
                        redisUtils.del(NoahEnum.GPSKEYDIR.getCode() + NoahEnum.GPSNUMKEY.getCode());
                    }
                    Sconfig sconfig2 = sconfigMapper.getMapkey("Y", sconfig.getType());
                    if (ObjectUtils.isEmpty(sconfig2)) {
                        return new Result(false, StatusCode.ERROR, "修改失败");
                    }
                    boolean inres = redisUtils.lSet(NoahEnum.GPSKEYDIR.getCode() + NoahEnum.GPSKEY.getCode(), sconfig2);
                    if (!inres) {
                        return new Result(false, StatusCode.ERROR, "修改失败");
                    }
                }
            } else {
                Sconfig sconfig2 = sconfigMapper.getMapkey("Y", sconfig.getType());
                if (ObjectUtils.isEmpty(sconfig2)) {
                    return new Result(false, StatusCode.ERROR, "修改失败");
                }
                boolean inres = redisUtils.lSet(NoahEnum.GPSKEYDIR.getCode() + NoahEnum.GPSKEY.getCode(), sconfig2);
                if (!inres) {
                    return new Result(false, StatusCode.ERROR, "修改失败");
                }
                if (redisUtils.hasKey(NoahEnum.GPSKEYDIR.getCode() + NoahEnum.GPSNUMKEY.getCode())) {
                    redisUtils.del(NoahEnum.GPSKEYDIR.getCode() + NoahEnum.GPSNUMKEY.getCode());
                }
            }
        }
        return new Result(true, StatusCode.OK, "操作成功");
    }

}
